package com.gps.pe.dao;

import static com.gps.pe.utility.DAOUtil.prepareStatement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.gps.pe.exception.DAOException;
import com.gps.pe.model.Role;
import com.gps.pe.utility.GetSQLQuery;

/**
 * Class have implementation details of Role DAO
 * 
 * @author GPS
 *
 */
public class RoleDAOImpl implements IRoleDAO {
	private static final Logger LOGGER = Logger.getLogger(RoleDAOImpl.class);
	private Connection connection = null;

	RoleDAOImpl(Connection connection) {
		this.connection = connection;
	}

	/**
	 * Method to get Role by Id
	 */
	public Role getRole(int roleId) throws DAOException {
		Role role = null;
		String sql = RoleQuery.GET_ROLE_BY_ID.getKeyVal();
		sql = GetSQLQuery.getQuery(sql);
		LOGGER.debug("SQL Query is [" + sql + "]");
		Object[] values = { roleId };
		try (PreparedStatement pstmt = prepareStatement(connection, sql, values);
				ResultSet resultSet = pstmt.executeQuery();) {
			
			if (resultSet.next()) {
				LOGGER.debug("Role found for Id [" + roleId + "]");
				role = mapRole(resultSet);
				LOGGER.debug("Fetched resultset for RoleId [" + roleId + "]");
			} else {
				LOGGER.debug("Role not found for Id [" + roleId + "]");
			}
		} catch (SQLException se) {
			LOGGER.error("SQLException to get Role for Id [" + roleId + "]" + se);
			throw new DAOException("Error to get Role by Id", se);
		}
		return role;
	}

	public List<Role> getAllRole() {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean addRole(Role role) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean updateRole(Role role) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean deleteRole(Role role) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * Map the current row of the given ResultSet to an Role.
	 * 
	 * @param resultSet
	 *            The ResultSet of which the current row is to be mapped to an
	 *            Role.
	 * @return The mapped Role from the current row of the given ResultSet.
	 * @throws SQLException
	 *             If something fails at database level.
	 */
	private static Role mapRole(final ResultSet resultSet) throws DAOException {
		Role role = new Role();
		try {
			role.setRoleId(resultSet.getInt("roleId"));
			role.setRoleName(resultSet.getString("roleName"));
			role.setReadAccess(resultSet.getString("readAccess"));
			role.setWriteAccess(resultSet.getString("writeAccess"));
		} catch (SQLException e) {
			LOGGER.error("SQLException to get Role from resultset");
			throw new DAOException("Problem to get Role from resultset", e);
		}
		return role;
	}

	/**
	 * Enum to get Query from property file
	 */
	private enum RoleQuery {
		GET_ROLE_BY_ID("pe.role.getRoleById");

		private String key;

		RoleQuery(String key) {
			this.key = key;
		}

		String getKeyVal() {
			return key;
		}
	}
}
